const db = require("./index")

// 封装msql语句 便于维护
let sqlExcute = function( sql, values ) {
  // 返回一个 Promise
  return new Promise(( resolve, reject ) => {
    db.getConnection(function(err, connection) {
      if (err) {
        reject( err )
      } else {
        connection.query(sql, values, (err, results) => {
          if ( err ) {
            reject( err )
          } else {
            resolve( results )
          }
          // 结束会话
          connection.release()
        })
      }
    })
  })
}

module.exports = sqlExcute
